package org.jeecg.chase.modules.merch.service.impl;


import org.jeecg.chase.modules.merch.entity.AdhMerchInfo;
import org.jeecg.chase.modules.merch.entity.AdhMerchUser;
import org.jeecg.chase.modules.merch.mapper.AdhMerchInfoMapper;
import org.jeecg.chase.modules.merch.mapper.AdhMerchUserMapper;
import org.jeecg.chase.modules.merch.service.IAdhMerchInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 商家管理
 * @Author: jeecg-boot
 * @Date:   2022-02-10
 * @Version: V1.0
 */
@Service
public class AdhMerchInfoServiceImpl extends ServiceImpl<AdhMerchInfoMapper, AdhMerchInfo> implements IAdhMerchInfoService {


	@Autowired
	private AdhMerchInfoMapper adhMerchInfoMapper;
	@Autowired
	private AdhMerchUserMapper adhMerchUserMapper;
	
	@Override
	@Transactional
	public void saveMain(AdhMerchInfo adhMerchInfo, List<AdhMerchUser> adhMerchUserList) {
		adhMerchInfoMapper.insert(adhMerchInfo);
		if(adhMerchUserList!=null && adhMerchUserList.size()>0) {
			for(AdhMerchUser entity:adhMerchUserList) {
				//外键设置
				entity.setMerchId(adhMerchInfo.getId());
				adhMerchUserMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AdhMerchInfo adhMerchInfo,List<AdhMerchUser> adhMerchUserList) {
		adhMerchInfoMapper.updateById(adhMerchInfo);
		
		//1.先删除子表数据
		adhMerchUserMapper.deleteByMainId(adhMerchInfo.getId());
		
		//2.子表数据重新插入
		if(adhMerchUserList!=null && adhMerchUserList.size()>0) {
			for(AdhMerchUser entity:adhMerchUserList) {
				//外键设置
				entity.setMerchId(adhMerchInfo.getId());
				adhMerchUserMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		adhMerchUserMapper.deleteByMainId(id);
		adhMerchInfoMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			adhMerchUserMapper.deleteByMainId(id.toString());
			adhMerchInfoMapper.deleteById(id);
		}
	}
	@Override
	public List<AdhMerchInfo> getMerchInfoByPhoneNumber(String phoneNumber){
		return this.adhMerchInfoMapper.getMerchInfoByPhoneNumber(phoneNumber);
	}
	@Override
	public List<Map<String,Object>> getFocusMerchInfoByUserIdAndMerchId( String merchId, String userId){
		return this.adhMerchInfoMapper.getFocusMerchInfoByUserIdAndMerchId(merchId, userId);
	}
	@Override
	public List<Map<String,Object>> getFocusMerchInfoByUserId( String userId){
		return this.adhMerchInfoMapper.getFocusMerchInfoByUserId( userId);
	}
}
